home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSSTATE.C < prev    next >
C/C++ Source or Header  |  1992-03-06  |  10KB  |  349 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsstate.c */
  21. /* Miscellaneous graphics state operators for Ghostscript library */
  22. #include "gx.h"
  23. #include "memory_.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"
  26. #include "gxmatrix.h"            /* for gzstate */
  27. #include "gzstate.h"
  28. #include "gzdevice.h"
  29. #include "gzcolor.h"            /* requires gxdevice.h */
  30. #include "gzht.h"
  31. #include "gzline.h"
  32. #include "gzpath.h"
  33.  
  34. /* Forward references */
  35. private gs_state *alloc_gstate(P4(proc_alloc_t, proc_free_t, const gs_state *, const char *));
  36. private int alloc_gstate_contents(P1(gs_state *));
  37. private void free_gstate_contents(P1(gs_state *));
  38. private void copy_gstate_contents(P2(gs_state *pto, const gs_state *pfrom));
  39.  
  40. /* Allocate and free a structure */
  41. #define alloc_struct(pgs,typ,cname)\
  42.   (typ *)(*(pgs)->memory_procs.alloc)(1, sizeof(typ), cname)
  43. #define free_struct(pgs,ptr,cname)\
  44.   (*(pgs)->memory_procs.free)((char *)(ptr), 1, sizeof(*(ptr)), cname)
  45.  
  46. /* ------ Operations on the entire graphics state ------ */
  47.  
  48. /* Allocate and initialize a graphics state. */
  49. private float
  50. null_transfer(gs_state *pgs, floatp gray)
  51. {    return gray;
  52. }
  53. gs_state *
  54. gs_state_alloc(proc_alloc_t palloc, proc_free_t pfree)
  55. {    register gs_state *pgs = alloc_gstate(palloc, pfree, (gs_state *)0, "gs_state_alloc");
  56.     if ( pgs == 0 ) return 0;
  57.     pgs->saved = 0;
  58.     /* Initialize things not covered by initgraphics */
  59.     gx_path_init(pgs->path, &pgs->memory_procs);
  60.     gx_path_init(&pgs->clip_path->path, &pgs->memory_procs);
  61.     gx_clip_list_init(&pgs->clip_path->list);
  62.     pgs->halftone->width = pgs->halftone->height =
  63.         pgs->halftone->order_size = 0;
  64.     gs_sethalftonephase(pgs, 0, 0);
  65.     /* Initialize the color so that gx_remap_color won't crash. */
  66.     gx_set_gray_only(pgs->color, (color_param)0);
  67.     gs_settransfer(pgs, null_transfer);
  68.     gs_nulldevice(pgs);
  69.     gs_setflat(pgs, 1.0);
  70.     gs_setstrokeadjust(pgs, 1);
  71.     /****** What about the font ? ******/
  72.     pgs->in_cachedevice = pgs->in_charpath = 0;
  73.     pgs->level = 0;
  74.     if ( gs_initgraphics(pgs) < 0 )
  75.        {    /* Something went very wrong */
  76.         return 0;
  77.        }
  78.     return pgs;
  79. }
  80.  
  81. /* Free a graphics state */
  82. int
  83. gs_state_free(gs_state *pgs)
  84. {    free_gstate_contents(pgs);
  85.     free_struct(pgs, pgs, "gs_state_free");
  86.     return 0;
  87. }
  88.  
  89. /* Save the graphics state */
  90. int
  91. gs_gsave(gs_state *pgs)
  92. {    gs_state *pnew = alloc_struct(pgs, gs_state, "gs_gsave");
  93.     if ( pnew == 0 )
  94.         return_error(gs_error_VMerror);
  95.     *pnew = *pgs;
  96.     if ( alloc_gstate_contents(pgs) < 0 )
  97.        {    *pgs = *pnew;        /* undo partial alloc */
  98.         free_struct(pgs, pnew, "gs_gsave");
  99.         return_error(gs_error_VMerror);
  100.        }
  101.     copy_gstate_contents(pgs, pnew);
  102.     pgs->saved = pnew;
  103.     pgs->level++;
  104.     return 0;
  105. }
  106.  
  107. /* Restore the graphics state. */
  108. int
  109. gs_grestore(gs_state *pgs)
  110. {    gs_state *saved = pgs->saved;
  111.     if ( !saved ) return gs_gsave(pgs);    /* shouldn't happen */
  112.     free_gstate_contents(pgs);
  113.     *pgs = *saved;
  114.     free_struct(pgs, saved, "gs_grestore");
  115.     return (pgs->saved == 0 ? gs_gsave(pgs) : 0);
  116. }
  117.  
  118. /* Restore to the bottommost graphics state. */
  119. int
  120. gs_grestoreall(gs_state *pgs)
  121. {    if ( !pgs->saved ) return gs_gsave(pgs);    /* shouldn't happen */
  122.     while ( pgs->saved->saved ) gs_grestore(pgs);
  123.     return gs_grestore(pgs);
  124. }
  125.  
  126. /* Allocate and return a new graphics state. */
  127. gs_state *
  128. gs_gstate(gs_state *pgs)
  129. {    gs_state *pnew = alloc_gstate(pgs->memory_procs.alloc, pgs->memory_procs.free, pgs, "gs_gstate");
  130.     if ( pnew == 0 ) return 0;
  131.     copy_gstate_contents(pnew, pgs);
  132.     pnew->saved = 0;
  133.     return pgs;
  134. }
  135.  
  136. /* Copy the current graphics state to a previously allocated one. */
  137. int
  138. gs_currentgstate(gs_state *pto, const gs_state *pgs)
  139. {    /* We have to copy both the scalar and composite parts */
  140.     /* of the state. */
  141.     gs_state sgs;
  142.     sgs = *pto;
  143.     *pto = *pgs;
  144.     /* Put back the composite part pointers. */
  145. #define gcopy(element)\
  146.     pto->element = sgs.element
  147.     gcopy(path);
  148.     gcopy(clip_path);
  149.     gcopy(line_params);
  150.     gcopy(halftone);
  151.     gcopy(color);
  152.     gcopy(dev_color);
  153.     gcopy(transfer);
  154.     gcopy(device);
  155. #undef gcopy
  156.     copy_gstate_contents(pto, pgs);
  157.     return 0;
  158. }
  159.  
  160. /* Restore the current graphics state from a previously allocated one. */
  161. int
  162. gs_setgstate(gs_state *pgs, const gs_state *pfrom)
  163. {    /* The implementation is the same as currentgstate, */
  164.     /* except we must preserve the saved pointer and the level. */
  165.     gs_state *saved = pgs->saved;
  166.     int level = pgs->level;
  167.     int code = gs_currentgstate(pgs, pfrom);
  168.     if ( code < 0 ) return code;
  169.     pgs->saved = saved;
  170.     pgs->level = level;
  171.     return 0;
  172. }
  173.  
  174. /* Swap the saved pointer of the graphics state. */
  175. /* This is provided only for save/restore. */
  176. gs_state *
  177. gs_state_swap_saved(gs_state *pgs, gs_state *new_saved)
  178. {    gs_state *saved = pgs->saved;
  179.     pgs->saved = new_saved;
  180.     return saved;
  181. }
  182.  
  183. /* Swap the contents of two graphics states, except for the saved pointer. */
  184. /* This is provided only for save/restore. */
  185. void
  186. gs_state_swap(gs_state *p1, gs_state *p2)
  187. {    gs_state temp;
  188.     temp = *p1, *p1 = *p2, *p2 = temp;
  189.     /* Restore the saved pointers. */
  190.     p2->saved = p1->saved;
  191.     p1->saved = temp.saved;
  192. }
  193.  
  194. /* ------ Operations on components ------ */
  195.  
  196. /* Reset most of the graphics state */
  197. int
  198. gs_initgraphics(register gs_state *pgs)
  199. {    int code;
  200.     gs_initmatrix(pgs);
  201.     if (    (code = gs_newpath(pgs)) < 0 ||
  202.         (code = gs_initclip(pgs)) < 0 ||
  203.         (code = gs_setlinewidth(pgs, 1.0)) < 0 ||
  204.         (code = gs_setlinecap(pgs, gs_cap_butt)) < 0 ||
  205.         (code = gs_setlinejoin(pgs, gs_join_miter)) < 0 ||
  206.         (code = gs_setdash(pgs, (float *)0, 0, 0.0)) < 0 ||
  207.         (code = gs_setgray(pgs, 0.0)) < 0 ||
  208.         (code = gs_setmiterlimit(pgs, 10.0)) < 0
  209.        ) return code;
  210.     return 0;
  211. }
  212.  
  213. /* setflat */
  214. int
  215. gs_setflat(gs_state *pgs, floatp flat)
  216. {    if ( flat <= 0.2 ) flat = 0.2;
  217.     else if ( flat > 100 ) flat = 100;
  218.     pgs->flatness = flat;
  219.     return 0;
  220. }
  221.  
  222. /* currentflat */
  223. float
  224. gs_currentflat(const gs_state *pgs)
  225. {    return pgs->flatness;
  226. }
  227.  
  228. /* setstrokeadjust */
  229. int
  230. gs_setstrokeadjust(gs_state *pgs, int stroke_adjust)
  231. {    pgs->stroke_adjust = stroke_adjust;
  232.     return 0;
  233. }
  234.  
  235. /* currentstrokeadjust */
  236. int
  237. gs_currentstrokeadjust(const gs_state *pgs)
  238. {    return pgs->stroke_adjust;
  239. }
  240.  
  241. /* ------ Internal routines ------ */
  242.  
  243. /* Allocate a graphics state object and its contents, */
  244. /* optionally initializing it from an existing object. */
  245. /* Return 0 if the allocation fails. */
  246. private gs_state *
  247. alloc_gstate(proc_alloc_t palloc, proc_free_t pfree, const gs_state *pold, const char *cname)
  248. {    gs_state *pgs = (gs_state *)(*palloc)(1, sizeof(gs_state), cname);
  249.     if ( pgs == 0 ) return 0;
  250.     if ( pold != 0 )
  251.         *pgs = *pold;
  252.     else
  253.         pgs->transfer = 0;
  254.     pgs->memory_procs.alloc = palloc;
  255.     pgs->memory_procs.free = pfree;
  256.     if ( alloc_gstate_contents(pgs) < 0 )
  257.        {    free_struct(pgs, pgs, cname);
  258.         return 0;
  259.        }
  260.     return pgs;
  261. }
  262.  
  263. /* Allocate the contents of a graphics state object. */
  264. /* Return -1 if the allocation fails. */
  265. /* Note that the contents have been smashed in this case. */
  266. private int
  267. alloc_gstate_contents(register gs_state *pgs)
  268. {    proc_alloc_t palloc = pgs->memory_procs.alloc;
  269.     static char *cname = "alloc_gstate_contents";
  270. #define galloc(element,type,fail)\
  271.     if ( (pgs->element = (type *)(*palloc)(1, sizeof(type), cname)) == 0 ) goto fail
  272.     galloc(path, gx_path, up);
  273.     galloc(clip_path, gx_clip_path, ucp);
  274.     galloc(line_params, line_params, ulp);
  275.     galloc(halftone, halftone_params, uht);
  276.     galloc(color, gs_color, uc);
  277.     galloc(dev_color, gx_device_color, udc);
  278.     if ( pgs->transfer != 0 )
  279.         pgs->transfer->ref_count++;
  280.     else
  281.        {    galloc(transfer, gx_transfer, ut);
  282.         pgs->transfer->ref_count = 1;
  283.        }
  284.     galloc(device, device, ud);
  285. #undef galloc
  286.     pgs->device_is_shared = 0;
  287.     return 0;
  288.     /* Undo partial allocations if an allocation failed. */
  289. #define gunalloc(element) free_struct(pgs, pgs->element, cname)
  290. ud:    gunalloc(transfer);
  291. ut:    gunalloc(dev_color);
  292. udc:    gunalloc(color);
  293. uc:    gunalloc(halftone);
  294. uht:    gunalloc(line_params);
  295. ulp:    gunalloc(clip_path);
  296. ucp:    gunalloc(path);
  297. up:    return -1;
  298. #undef gunalloc
  299. }
  300.  
  301. /* Free the contents of a graphics state, but not the state itself. */
  302. private void
  303. free_gstate_contents(gs_state *pgs)
  304. {    proc_free_t pfree = pgs->memory_procs.free;
  305.     static char *cname = "free_gstate_contents";
  306.     gx_cpath_release(pgs->clip_path);
  307.     gx_path_release(pgs->path);
  308. #define gfree(element)\
  309.     (*pfree)((char *)pgs->element, 1, sizeof(*pgs->element), cname)
  310.     if ( !pgs->device_is_shared )
  311.         gfree(device);
  312.     if ( !--(pgs->transfer->ref_count) )
  313.         gfree(transfer);
  314.     gfree(dev_color);
  315.     gfree(color);
  316.     gfree(halftone);
  317.     gfree(line_params);
  318.     gfree(clip_path);
  319.     gfree(path);
  320. #undef gfree
  321. }
  322.  
  323. /* Copy the composite parts of a graphics state. */
  324. private void
  325. copy_gstate_contents(gs_state *pto, const gs_state *pfrom)
  326. {
  327. #define gcopy(element)\
  328.     *pto->element = *pfrom->element
  329.     gcopy(path);
  330.     gcopy(clip_path);
  331.     gcopy(line_params);
  332.     gcopy(halftone);
  333.     gcopy(color);
  334.     gcopy(dev_color);
  335.     if ( pto->transfer != pfrom->transfer )
  336.        {    if ( !--(pto->transfer->ref_count) )
  337.            {    /* We could just copy the contents, but */
  338.             /* we'd rather free the storage and hope that */
  339.             /* we won't have to reallocate later. */
  340.             free_struct(pto, pto->transfer, "copy gstate");
  341.            }
  342.         (pto->transfer = pfrom->transfer)->ref_count++;
  343.        }
  344.     gcopy(device);
  345. #undef gcopy
  346.     gx_path_share(pto->path);
  347.     gx_cpath_share(pto->clip_path);
  348. }
  349.